<?php

/**
* @current fashionweek.php
* @author   LiuYang <liu_yang@haibao.com>
* @date 2016年9月2日 下午6:06:48
*/
namespace haibao\cms\data;

use haibao\cms\model\filter\fashionweek\FashionWeek as FashionWeekFilter;

class FashionWeek extends \haibao\cms\data\BaseMysql{
	
	public function __construct(){
		parent::__construct('\haibao\cms\model\data\fashionweek\FashionWeek');
	}
	
	public function addFashionData($model){
	    $this->beginTranscation();
	    try{
	        $this->add($model);
	    } catch(\Exception $e){
	        throw $e;
	    }
	    $this->endtranscation();
	    return $model;
	}
	
	public function editFashionData($model){
	    $fashionModel = $this->packFashionFilter($model);
	    $this->beginTranscation();
	    try{
	        $this->updateById($model->Id,$fashionModel);
	    } catch(\Exception $e){
	        throw $e;
	    }
	    $this->endtranscation();
	}
	private function packFashionFilter($model){
	    $fashionFilter = new FashionWeekFilter();
	    $fashionFilter = array(
	        FashionWeekFilter::CONDITION_FIELD_NAME_EVENTID =>$model->EventId,
	        FashionWeekFilter::CONDITION_FIELD_NAME_IMAGEID =>$model->ImageId,
	        FashionWeekFilter::CONDITION_FIELD_NAME_STATUS =>$model->Status,
	        FashionWeekFilter::CONDITION_FIELD_NAME_CREATEBY =>$model->CreateBy,
	        FashionWeekFilter::CONDITION_FIELD_NAME_CREATETIME =>$model->CreateTime,
	        FashionWeekFilter::CONDITION_FIELD_NAME_CLICKNUM =>$model->ClickNum,
	    );
	    return $fashionFilter;
	}
	
	public function updateClickNumById($fashionId){
	    $sql = "UPDATE FashionWeek SET ClickNum=ClickNum+1 WHERE Id=%s";
		$this->query($sql,array($fashionId));
	}
	
	public function getFshionWeekList($eventId,$page){
	    if($page == 0){
	        $limit = '';
	    }else{
	        $pager = ($page-1)*20;
	        $limit = 'LIMIT '.$pager.',20';
	    }
	    
	    $sql = '(SELECT a.Id,1 as Type,a.Title,a.PublishTime,a.PublishId,e.EventId FROM EventArticle as e LEFT JOIN Article as a on e.ArticleId=a.Id WHERE a.Published=1 AND a.`Status`=0 AND e.EventId='.$eventId.' AND a.`Level`>1)
                UNION
                (SELECT i.Id,2 as Type,i.Title,i.PublishTime,i.Id,ei.EventId from EventImageCollective as ei LEFT JOIN ImageCollective as i on ei.ImageCollectiveId=i.Id WHERE i.Published=1 AND i.`Status`=1 AND ei.EventId='.$eventId.' AND i.`Level`>1)
                ORDER BY PublishTime DESC  '.$limit;
	    $result = $this->query($sql);
	    $temp = array();
	    while ($row = $result->fetch_assoc()){
	        $temp[] = $row;
	    }
	    return $temp;
	}
	
	public function getArticlePro($articleIds){
	    $str = preg_replace('/(\d+)/', '%s', implode(',', $articleIds));
	    $sql = "SELECT * FROM ArticleProImage WHERE ArticleId IN (".$str.") AND PicType = 1 AND `Level` IN (0,5)";
	    $result = $this->query($sql,$articleIds);
	    $temp = array();
	    while ($row = $result->fetch_assoc()){
	        $temp[$row['ArticleId']] = $row;
	    }
	    return $temp;
	}
	
	public function getImageCollectivePro($imageCollectiveIds){
	    $str = preg_replace('/(\d+)/', '%s', implode(',', $imageCollectiveIds));
	    $sql = "SELECT * FROM ImageCollectivePro WHERE ImageCollectiveId IN (".$str.") AND ImageType IN(0,5)";
	    $result = $this->query($sql, $imageCollectiveIds);
	    $temp = array();
	    while ($row = $result->fetch_assoc()){
	        $temp[$row['ImageCollectiveId']] = $row;
	    }
	 /*    if(empty($temp)){
	    	$sql = "SELECT * FROM ImageCollectivePro WHERE ImageCollectiveId IN (".$str.") AND ImageType = 0";
	    	$result = $this->query($sql, $imageCollectiveIds);
	    	$temp = array();
	    	while ($row = $result->fetch_assoc()){
	    		$temp[$row['ImageCollectiveId']] = $row;
	    	}
	    } */
	    return $temp;
	}
}